﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Forms;

using System.Drawing;
using System.Drawing.Imaging;

using MessagingToolkit;
using MessagingToolkit.QRCode.Codec;
using MessagingToolkit.QRCode.Codec.Data;
using MessagingToolkit.QRCode.Helper;
using Microsoft.Samples.Kinect.SkeletonBasics;
using Renci.SshNet;
using System.Net;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Security.Permissions;

namespace Microsoft.Samples.Kinect.SkeletonBasics
{
    /// <summary>
    /// Interaction logic for ThirdPage.xaml
    /// </summary>
    public partial class ThirdPage : Page
    {
        private DateTime endTime;

        public ThirdPage()
        {
            InitializeComponent();
        }

        //String stringPath = "Pictures/myPicture.jpg";
        //    Uri imageUri = new Uri(stringPath, UriKind.Relative);
        //    BitmapImage imageBitmap = new BitmapImage(imageUri);
        //    Image myImage = new Image();
        //    myImage.Source = imageBitmap;
        
        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            if (MainWindow.snapShotURL != "")
            {

                const int port = 22;
                const string host = "pikkumyy.lpt.fi";
                const string username = "robokinetic";
                const string password = "Koodaus1";
                const string workingdirectory = "/home/robokinetic/public_html/kuvat/";
                string uploadfile = MainWindow.snapShotURL;

                using (var client = new SftpClient(host, port, username, password))
                {
                    client.Connect();
                    Console.WriteLine("Connected to {0}", host);

                    client.ChangeDirectory(workingdirectory);
                    Console.WriteLine("Changed directory to {0}", workingdirectory);

                    var listDirectory = client.ListDirectory(workingdirectory);
                    Console.WriteLine("Listing directory:");
                    foreach (var fi in listDirectory)
                    {
                        Console.WriteLine(" - " + fi.Name);
                    }

                    using (var fileStream = new FileStream(uploadfile, FileMode.Open))
                    {
                        Console.WriteLine("Uploading {0} ({1:N0} bytes)",
                                            uploadfile, fileStream.Length);
                        client.BufferSize = 4 * 1024; // bypass Payload error large files
                        client.UploadFile(fileStream, System.IO.Path.GetFileName(uploadfile));
                    }
                }
                
                BitmapImage imageBitmap = new BitmapImage(new Uri(MainWindow.snapShotURL, UriKind.Absolute));
                snapshot.Source = imageBitmap;
          
                
                double wh = System.Windows.SystemParameters.PrimaryScreenHeight;

                double gs =  wh/1080;

                qrkoodi.Width *= gs;
                qrkoodi.Height *= gs;

                double qrml = qrkoodi.Margin.Left * gs;
                double qrmt = qrkoodi.Margin.Top * gs;
                double qrmr = qrkoodi.Margin.Right * gs;
                double qrmb = qrkoodi.Margin.Bottom * gs;

                qrkoodi.Margin = new Thickness( qrml, qrmt, qrmr, qrmb );
                qrkoodi.InvalidateVisual();

                snapshot.Width *= gs;
                snapshot.Height *= gs;

                shotcontainer.Width *= gs;
                shotcontainer.Height *= gs;

                double snml = shotcontainer.Margin.Left * gs;
                double snmt = shotcontainer.Margin.Top * gs;
                double snmr = shotcontainer.Margin.Right * gs;
                double snmb = shotcontainer.Margin.Bottom * gs;

                shotcontainer.Margin = new Thickness(snml, snmt, snmr, snmb);

                snapshot.InvalidateVisual();
                shotcontainer.InvalidateVisual();

                createQR("http://pikkumyy.lpt.fi/~robokinetic?name="+MainWindow.photoName);

                MainWindow.changeSnapShot("");

                Countdown(60, TimeSpan.FromSeconds(1), 0);
            }
        }

        public void Countdown(int count, TimeSpan interval, int luku)
        {
            var dt = new System.Windows.Threading.DispatcherTimer();
            dt.Interval = interval;
            dt.Tick += (_, a) =>
            {
                if (count-- == 0)
                {
                    dt.Stop();
                    if (luku == 0)
                    {
                        MainWindow.kalleEnding();
                        kiitos.Visibility = Visibility.Visible;
                        Countdown(5, TimeSpan.FromSeconds(1), 1);
                    }
                    if (luku == 1)
                    {
                        MainWindow.restartProgram();
                    }
                }
                 
            };
          
            dt.Start();
        }

        private void createQR(string code)
        {
            string data = code;
            QRCodeEncoder encoder = new QRCodeEncoder();
            encoder.QRCodeEncodeMode = MessagingToolkit.QRCode.Codec.QRCodeEncoder.ENCODE_MODE.BYTE;
            encoder.QRCodeErrorCorrect = MessagingToolkit.QRCode.Codec.QRCodeEncoder.ERROR_CORRECTION.H;

            int qrscale = Convert.ToInt16(4); // KOKO!!!!
            encoder.QRCodeScale = qrscale;

            int qrversion = Convert.ToInt16(0); // versio 0-40
            encoder.QRCodeVersion = qrversion;

            System.Drawing.Image qrImage = encoder.Encode(data);
            BitmapSource bitmapSource = ImageHelper.ToBitmapSource(qrImage);
            qrkoodi.Source = bitmapSource;
        }



    }


}
